package Model;

import java.awt.List;
import java.text.NumberFormat;
import java.util.ArrayList;

public class GioHang {
	private ArrayList<Mobile> listmobile;
	private User user;
	private double sum = 0;

	// public GioHang(ArrayList<Mobile> listmobile, User user) {
	// super();
	// this.listmobile = listmobile;
	// this.user = user;
	// }

	public GioHang() {
		listmobile = new ArrayList<Mobile>();
	}

	public void update(Mobile linemb) {
		int id = linemb.getMobileID();
		int quantity = linemb.getSoLuong();

		for (int i = 0; i < listmobile.size(); i++) {
			Mobile lineItem = listmobile.get(i);
			if (lineItem.getMobileID() == id) {
				lineItem.setSoLuong(quantity);
				return;
			}
		}
	}

	public void add(Mobile linemb) {
		int id = linemb.getMobileID();
//		int quantity = linemb.getSoLuong();

		for (int i = 0; i < listmobile.size(); i++) {
			Mobile lineItem = listmobile.get(i);
			if (lineItem.getMobileID() == id) {
				lineItem.setSoLuong(lineItem.getSoLuong()+1);
				return;
			}
		}

		listmobile.add(linemb);
	}

	// public void add(Mobile linemb){
	// System.out.println("hhhhhhhhh");
	// int id = linemb.getMobileID();
	// int quantity = linemb.getSoLuong();
	// System.out.println("q: "+quantity);
	//
	// for (int i = 0; i < listmobile.size(); i++) {
	// Mobile lineItem = listmobile.get(i);
	// // int s=lineItem.getSoLuong();
	// if(lineItem.getMobileID()==id){
	// if(lineItem.getSoLuong() !=quantity){
	// System.out.println("vao 1");
	// lineItem.setSoLuong(quantity);
	// }
	// else {
	// System.out.println("vao 2: "+lineItem.getSoLuong());
	// // lineItem.setSoLuong(++quantity);
	// // lineItem.setSoLuong(lineItem.getSoLuong()+quantity);
	// }
	// return;
	// }}
	//
	// listmobile.add(linemb);
	// }

	public ArrayList<Mobile> getGioHang() {
		return listmobile;
	}

	public void remove(Mobile mb) {
		int mobileID = mb.getMobileID();
		for (int i = 0; i < listmobile.size(); i++) {
			Mobile lineItem = listmobile.get(i);
			if (lineItem.getMobileID() == mobileID) {
				listmobile.remove(i);
				return;
			}

		}
	}

	public String getTotal() {
		double tongTien = 0;
		for (Mobile lineMobile : listmobile) {
			tongTien = tongTien + lineMobile.getTotal();
		}
		sum = tongTien;
		NumberFormat nf = NumberFormat.getInstance();
		return nf.format(tongTien);
	}

	public double getSum() {
		return sum;
	}

	public ArrayList<Mobile> capNhatSoLuongSanPham() {
		return listmobile;

	}

	public ArrayList<Mobile> themSanPham() {
		return listmobile;

	}

	public ArrayList<Mobile> xoaMotSanPham() {
		return listmobile;

	}

	public ArrayList<Mobile> xoaTatCa() {
		return listmobile;

	}

	public ArrayList<Mobile> getListmobile() {
		return listmobile;
	}

	public void setListmobile(ArrayList<Mobile> listmobile) {
		this.listmobile = listmobile;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
